package com.zy.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.zy.constant.MessageConstant;
import com.zy.entity.Result;
import com.zy.pojo.OrderSetting;
import com.zy.service.OrderSettingService;
import com.zy.utils.POIUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;


@RestController
@PreAuthorize("hasAuthority('ORDERSETTING')")
@RequestMapping("/ordersetting")
public class OrderSettingController {

	@Reference
	private OrderSettingService orderSettingService;

	@RequestMapping("/upload")
	public Result upload(@RequestParam("excelFile") MultipartFile excelFile){
		try {
			//读取excel文件
			List<String[]> strings = POIUtils.readExcel(excelFile);
			//对象转变
			if(strings!=null && strings.size() > 0){
				ArrayList<OrderSetting> orderSettings = new ArrayList<>();
				for (String[] string : strings) {
					OrderSetting orderSetting = new OrderSetting(new Date(string[0]), Integer.parseInt(string[1]));
					orderSettings.add(orderSetting);
				}
				//调用service继续处理
				orderSettingService.add(orderSettings);
			}
			return new Result(true,MessageConstant.IMPORT_ORDERSETTING_SUCCESS);


		} catch (IOException e) {
			return new Result(false, MessageConstant.IMPORT_ORDERSETTING_FAIL);

		}
	}

	@RequestMapping("/getOrderSettingByMonth")
	public Result getOrderSettingByMonth(@RequestParam("date") String string){
		try {
			List<Map> list =orderSettingService.getOrderSettingByMonth(string);
			return new Result(true, MessageConstant.GET_ORDERSETTING_SUCCESS,list);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false, MessageConstant.GET_ORDERSETTING_FAIL);
		}
	}

	@RequestMapping("/editNumberByDate")
	public Result editNumberByDate(@RequestBody OrderSetting orderSetting){
		try {
			orderSettingService.editNumberByDate(orderSetting);
			return new Result(true,MessageConstant.ORDERSETTING_SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false,MessageConstant.ORDERSETTING_FAIL);
		}

	}

}
